package api_day05;
/*
    栈的链式结构实现
        只需要在头节点后插入新的元素即可！！
 */
public class LinkedStack {
    public Node head;
    private int capcity;
    private int size;
    LinkedStack(){
        head = new Node();
        capcity = 10;
        size = 0;
    }
    public void push(Object obj){
        if (isFull()) return;
        Node newNode = new Node(obj);
        newNode.next = head.next;
        head.next = newNode;
        size++;
    }
    public Object pop(){
        //要判断栈顶为空!!
        if (isEmpty()) return null;
        Object o = head.next.data;
        head.next = head.next.next;
        size--;
        System.out.println(o);
        return o;
    }
    public boolean isEmpty(){
        return size == 0;
    }
    public boolean isFull(){
        return size == capcity;
    }
    @Override
    public String toString() {
        StringBuilder out = new StringBuilder("从栈顶至栈底：");
        Node current = head;
        while (current.next != null){
            current = current.next;
            out.append(current.data + ",");
        }
        out.deleteCharAt(out.length()-1);
        return out.toString();
    }

    private class Node{
        Object data;
        Node next;
        public Node() {
        }
        public Node(Object data) {
            this.data = data;
        }
    }
}
